package com.itbbs.user.controller;

import com.itbbs.exception.CustomizeException;
import com.itbbs.result.Result;
import com.itbbs.result.ResultCodeEnum;
import com.itbbs.user.service.api.UserService;
import com.itbbs.vo.user.UserLoginVo;
import com.itbbs.vo.user.UserRegisteredVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author ysk
 * @version 1.0.1
 * @Description 用户服务的接口
 * @createTime 2021年06月03日 11:34:00
 */
@Api(tags = "用户服务接口")
@RestController
@RequestMapping("/user/client")
public class UserCertificationController {

    @Autowired
    private UserService userService;


    /**
     * 用户登陆的接口
     *
     * @param loginVo 封装了用户登陆的信息
     * @return 返回一个由map集合组成的数据
     */
    @ApiOperation(value = "登陆")
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody UserLoginVo loginVo) {
        Map<String, Object> map = userService.loginUser(loginVo);
        return Result.ok(map);
    }


    /**
     * 用户注册的接口
     *
     * @param registeredVo 封装了用户注册的信息
     */
    @ApiOperation(value = "注册")
    @PostMapping("/registered")
    public Result registered(@RequestBody UserRegisteredVo registeredVo) {
        userService.registrationUser(registeredVo);
        return Result.ok();
    }

    @ApiOperation(value = "验证账号")
    @GetMapping("/accountVerify/{account}")
    public Result accountVerify(@PathVariable("account") String account){
        // 判断账号是否合法
        if (account.length() < 3){
            throw new CustomizeException(ResultCodeEnum.PARAM_ERROR);
        }
        userService.ifExistsUserbyAccount(account);
        return Result.ok();
    }

    @ApiOperation(value = "发送邮件")
    @PostMapping("/sendEmailGetUser/{account}/{type}")
    public Result sendEmailGetUser(@PathVariable("account") String account,@PathVariable("type") Integer type){
        userService.sendEmail(account,type);
        return Result.ok();
    }

    @ApiOperation(value = "重置密码")
    @PutMapping("/resetPass/{email}/{password}")
    public Result resetPass(@PathVariable("email") String email,@PathVariable("password") Integer password){
        userService.resetPassByEmail(email,password);
        return Result.ok();
    }

}
